// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    data: {
        info: [],
        detail: [],
        stateOption: {},
        fileList: []
    },
    goto(e) {
      return api.goto(e)
    },
    main: async function () {
        await this.getState()
    },
    getState: async function () {
        let res = await api.curlGet('?cla=equipmentInspection&fun=editOption')
        if (res.code === 1) {
            this.setData({
              stateOption: res.data.state
            })
        }
    },
    // 更新本地数据
    getDetail: async function (id) {
        let res = await api.curlPost('?cla=equipmentInspection&fun=detail', {id: id})
        if (res.code === 1) {
            const resp = res.data.result
            this.setData({
                info: resp.info,
                detail: resp.detail,
                fileList:resp.attachFile?resp.attachFile:[]
            })
        }
    },
    choiceEquipment: async function (eqid, id = '') {
      let res = await api.curlPost('?cla=equipmentInspection&fun=choiceEquipment', {eqid: eqid, id: id})
      if (res.code === 1) {
        const resp = res.data.result
        this.setData({
            info: resp.info,
            detail: resp.detail,
        })
      }
    },
    onShow: async function () {
        let that = this
        const equipment = wx.getStorageSync('equipment')
        if (equipment) {
          await that.choiceEquipment(equipment.id, that.data.info.id)
          wx.removeStorageSync('equipment')
        }
    },
    onUnload: async function () {
      wx.removeStorageSync('equipment')
    },
    // 渲染
    onLoad: async function (options) {
        let that = this,
            id = options.id ? options.id : '',
            eqid = options.eqid ? options.eqid : ''
        if(eqid){
          await that.choiceEquipment(eqid)
        } else if (id) {
          await that.getDetail(id)
        }
        await that.main()
    },
    formSubmit: async function (e) {
        let obj = e.detail.value
        if (obj) {
            // obj['detail'] = api.arrayToJson(problem)
            let fileList = this.data.fileList;
            let fileId = [];
            fileList.forEach((v,i)=>{
              fileId.push(v.id)
            })
            obj.fileId = JSON.stringify(fileId);
            let res = await api.curlPost('?cla=equipmentInspection&fun=edit', obj)
            if (res.code === 1) {
                wx.navigateTo({
                    url: 'detail?id=' + res.data
                })
                // wx.removeStorageSync('problemData')
                api.showToast('新增成功', 'success')
            } else {
                api.showToast(res.warn)
            }
        }
    },
    /**上传文件 */
  afterRead(event) {
    let that = this;
    const { file } = event.detail;
    api.uploadFileCRM(app.root+'?cla=fileUploadApp&fun=uploadFile',file,{power: 'equipmentInspection',fileType:'设备巡检'}).then((res)=>{
      // 上传完成需要更新 fileList
      const { fileList = [] } = that.data;
        fileList.push({ ...file, url: res.result.src,id:res.result.id });
        that.setData({ fileList });
      })
  },
  /**删除文件 */
  delFile(event){
    let index = event.detail.index;
    let fileList = this.data.fileList;
    let newFileList=[];
    let id = '';
    fileList.forEach((v,i)=>{
      if(i==index){
        id=v.id;
      }else{
        newFileList.push(v);
      }
    })
    let that = this;
    api.curlPost('?cla=fileUploadApp&fun=delFile',{id:id,power:'equipmentInspection'}).then((res)=>{
      that.setData({
        fileList:newFileList
      })
    })
    
  },
});
